package cn.michael.dev.data.structure.linked.list;

import cn.michael.dev.entity.ListNode;

/**
 * Created by hufenggang on 2021/8/27.
 */
public class Leetcode21 {

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode newNode = new ListNode(-1);
        ListNode currentNew = newNode;

        while (l1 != null && l2 != null) {
            if (l1.val <= l2.val) {
                currentNew.next = l1;
                l1 = l1.next;
            } else {
                currentNew.next = l2;
                l2 = l2.next;
            }
            currentNew = currentNew.next;
        }

        currentNew.next = l1 == null ? l2 : l1;
        return newNode.next;
    }
}
